#pragma once
#pragma once
#include<iostream>
#include"reverse_iterator.h"
using namespace std;
namespace zjs
{
	template <class T>
	struct list_node {
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& data = T())
			:_data(data)
			, _next(nullptr)
			, _prev(nullptr)
		{}
	};

	template <class T, class Ref, class Ptr >
	struct __list_iterator {
		typedef list_node<T> Node;
		typedef __list_iterator self;
		Node* node;

		__list_iterator(Node* x)
			:node(x)
		{}

		self& operator++()
		{
			node = node->_next;
			return *this;
		}

		self& operator--()
		{
			node = node->_prev;
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			node = node->_next;
			return tmp;
		}

		self operator--(int)
		{
			self tmp(*this);
			node = node->_prev;
			return tmp;
		}

		Ref operator*()
		{
			return node->_data;
		}

		bool operator==(const self& It) const
		{
			return node == It.node;
		}

		bool operator!=(const self& It) const
		{
			return node != It.node;
		}

		Ptr operator->()
		{
			return &node->_data;
		}
	};

	template <class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;


		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(--end());
		}

		reverse_iterator rbegin()
		{
			return --end();
		}

		const_reverse_iterator rend() const
		{
			return end();
		}

		reverse_iterator rend()
		{
			return end();
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			_size = 0;
			empty_init();
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		list(const list<T>& tmp)
			:_head(nullptr)
			, _size(0)
		{
			empty_init();
			for (auto x : tmp)
			{
				push_back(x);
			}
		}

		void swap(list& tmp)
		{
			std::swap(_head, tmp._head);
			std::swap(_size, tmp._size);
		}

		list<T>& operator=(list<T> tmp)
		{
			swap(tmp);
			return *this;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		iterator begin()
		{
			//return iterator(_head->_next);
			return _head->_next;
		}

		const_iterator end() const
		{
			//return iterator(_head);
			return _head;
		}

		iterator end()
		{
			//return iterator(_head);
			return _head;
		}

		void push_back(const T& x)
		{
			//Node* tail = _head->_prev;
			//Node* newnode = new Node(x);
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos.node;
			Node* pre = cur->_prev;
			Node* newnode = new Node(x);
			pre->_next = newnode;
			newnode->_prev = pre;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
			return newnode;
		}


		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator erase(iterator pos)
		{
			//assert(pos.node = _head);
			Node* cur = pos.node;
			Node* pre = cur->_prev;
			Node* next = cur->_next;
			pre->_next = next;
			next->_prev = pre;
			delete cur;
			_size--;
			return next;
		}

		size_t size() const
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

	template<typename T>
	void print_list(const list<T>& s)
	{
		typename list<T>::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	template<typename container>
	void print_container(const container& s)
	{
		typename container::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
}